//
//  RecordDetail_VC.m
//  Run
//
//  Created by sxz on 16/4/7.
//  Copyright © 2016年 BetaTheta. All rights reserved.
//

#import "RecordDetail_VC.h"
#import <MAMapKit/MAMapKit.h>
#import "PointsModel.h"
#import "BigView.h"

@interface RecordDetail_VC ()<MAMapViewDelegate>

@property (nonatomic, strong) MAMapView *mapView;
@property (nonatomic, strong) NSMutableArray *points;
@property (nonatomic, strong) BigView *bigView;
@property (nonatomic, assign) NSInteger type;

@end

@implementation RecordDetail_VC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.view.backgroundColor = [UIColor whiteColor];
    [self createMap];
    [self createBigView];
    
}

#pragma mark - 创建运动文字
- (void)createBigView {
    
    self.bigView = [[BigView alloc] initWithFrame:CGRectMake(0, HEIGHT * 0.7 - 64, WIDTH, HEIGHT * 0.3 + 1)];
    self.bigView.backgroundColor = [UIColor colorWithRed:0.1686 green:0.1686 blue:0.1961 alpha:1.0];
    self.bigView.distanceNum.textColor = [UIColor whiteColor];
    self.bigView.speedNum.textColor = [UIColor whiteColor];
    self.bigView.timeNum.textColor = [UIColor whiteColor];
    
    self.bigView.distanceNum.text = [NSString stringWithFormat:@"%.2f", self.data.distance.doubleValue / 1000.0];
    self.bigView.speedNum.text = [NSString stringWithFormat:@"%.2f", self.data.speed.doubleValue];
    self.bigView.timeNum.text = [self convertTime:self.data.time.integerValue];
    
    [self.view addSubview:self.bigView];
    
}

#pragma mark - 创建地图
- (void)createMap {
    
    [MAMapServices sharedServices].apiKey = @"fa4dbd6236dd0c07acabc113594f482b";
    self.mapView = [[MAMapView alloc] initWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT * 0.7)];
    self.mapView.delegate = self;
    //    指南针
    self.mapView.showsCompass = NO;
    //    比例尺
    self.mapView.showsScale = NO;
    
    [self takePoints];

    [self.view addSubview:self.mapView];
    [self.mapView setZoomLevel:15.1 animated:YES];
    
}

#pragma mark - 获取地图点
- (void)takePoints {
    
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *path = [paths lastObject];
    
    NSString *str = [NSString stringWithFormat:@"run%ld%.f%.f", self.data.time.integerValue, self.data.distance.doubleValue, self.data.speed.doubleValue];
    path = [path stringByAppendingString:[NSString stringWithFormat:@"/%ld.plist", [str hash]]];
    NSLog(@"%@", path);
    
    self.points = [NSKeyedUnarchiver unarchiveObjectWithFile:path];
    PointsModel *firstPointModel = [self.points firstObject];
    NSLog(@"%@", firstPointModel);
    
    PointsModel *lastPointModel = [self.points lastObject];
    NSLog(@"%@", lastPointModel);
    
    CLLocationCoordinate2D firstPoint = CLLocationCoordinate2DMake(firstPointModel.latitude, firstPointModel.longitude);
    CLLocationCoordinate2D lastPoint = CLLocationCoordinate2DMake(lastPointModel.latitude, lastPointModel.longitude);
    
    [self.mapView setCenterCoordinate:firstPoint animated:YES];
    
    // 开始和结束大头针
    MAPointAnnotation *startAnnotation = [[MAPointAnnotation alloc] init];
    startAnnotation.coordinate = firstPoint;
    self.type = 0;
    [self.mapView addAnnotation:startAnnotation];
    
    MAPointAnnotation *lastAnnotation = [[MAPointAnnotation alloc] init];
    lastAnnotation.coordinate = lastPoint;
    self.type = 1;
    [self.mapView addAnnotation:lastAnnotation];
    
    // 画线
    CLLocationCoordinate2D polyLineCoords[self.points.count];
    for (int i = 0; i < self.points.count; i++) {
        PointsModel *model = self.points[i];
        CLLocationCoordinate2D point = CLLocationCoordinate2DMake(model.latitude, model.longitude);
        polyLineCoords[i] = point;
    }
    MAPolyline *polyLine = [MAPolyline polylineWithCoordinates:polyLineCoords count:self.points.count];
    [self.mapView addOverlay:polyLine];
    
}

// 自定义头针
- (MAAnnotationView *)mapView:(MAMapView *)mapView viewForAnnotation:(id<MAAnnotation>)annotation
{
    if ([annotation isKindOfClass:[MAPointAnnotation class]])
    {
        static NSString *reuseIndetifier = @"annotationReuseIndetifier";
        MAAnnotationView *annotationView = (MAAnnotationView *)[mapView dequeueReusableAnnotationViewWithIdentifier:reuseIndetifier];
        if (annotationView == nil)
        {
            annotationView = [[MAAnnotationView alloc] initWithAnnotation:annotation
                                                          reuseIdentifier:reuseIndetifier];
        }
        if (self.type == 0) {
             annotationView.image = [UIImage imageNamed:@"起"];
        } else {
            annotationView.image = [UIImage imageNamed:@"终"];
        }
        
        //设置中心点偏移，使得标注底部中间点成为经纬度对应点
        annotationView.centerOffset = CGPointMake(0, -18);
        return annotationView;
    }
    return nil;
}

#pragma mark - 地图划线
- (MAOverlayView *)mapView:(MAMapView *)mapView viewForOverlay:(id <MAOverlay>)overlay
{
    if ([overlay isKindOfClass:[MAPolyline class]])
    {
        MAPolylineView *polylineView = [[MAPolylineView alloc] initWithPolyline:overlay];
        
        polylineView.lineWidth = 3.f;
        polylineView.strokeColor = [UIColor colorWithRed:0 green:0 blue:1 alpha:1];
        polylineView.lineJoin = kCGLineJoinRound; //连接类型
        polylineView.lineCap = kCGLineCapRound; // 点的类型
        
        return polylineView;
    }
    return nil;
}

#pragma mark - 时间转换
- (NSString *)convertTime:(NSInteger)second {
    
    NSDateFormatter *fmt = [[NSDateFormatter alloc] init];
    second -= 3600 * 8;
    NSDate *d = [NSDate dateWithTimeIntervalSince1970:second];
    [fmt setDateFormat:@"HH:mm:ss"];
    NSString *showtimeNew = [fmt stringFromDate:d];
    return showtimeNew;
    
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
